import {
  get,
  set,
  cloneDeep,
  isEmpty,
  isNumber,
  difference,
  omit
} from 'lodash'

export const transformDateToObject = date => {
  const [from = '', to = ''] = date || []
  return {
    to,
    from
  }
}
export const transformDateToArrary = date => {
  let value = []
  const { to, from } = date || {}
  if (!isEmpty(from)) {
    value[0] = from
  }
  if (!isEmpty(to)) {
    value[1] = to
  }
  return value
}

export const transformOrderToArrary = order => {
  let value = []

  const { field, sort } = order || {}
  if (!isEmpty(field)) {
    set(value, '[0].field')
  }
  if (!isEmpty(sort)) {
    set(value, '[0].sort')
  }
  return value
}
export const transformOrderToObject = order => {
  let value = {}
  const field = get(order, '[0].field')
  const sort = get(order, '[0].sort')
  if (!isEmpty(field)) {
    value.field = field
  }
  if (!isEmpty(sort)) {
    value.sort = sort
  }
  return value
}

export const parseFast = (value, model = 'widget') => {
  const {
    filter: sourceFilter,
    order,
    integrationGroup = [],
    showAllIntegration = false,
    myFavorite,
    ...rest
  } = cloneDeep(value) || {}

  const {
    createDate,
    lastSyncDate,
    updateDate,
    reportNum,
    widgetNum,
    read,
    ready,
    received,
    type: sourceType = [],
    ...restFilter
  } = sourceFilter || {}
  const { min, max } = reportNum || {}
  const { min: widgetMin, max: widgetMax } = widgetNum || {}
  const { min: readMin, max: readMax } = read || {}
  const { min: readyMin, max: readyMax } = ready || {}
  const { min: receivedMin, max: receivedMax } = received || {}

  const filter = {
    createDate: transformDateToArrary(createDate),
    lastSyncDate: transformDateToArrary(lastSyncDate),
    updateDate: transformDateToArrary(updateDate),
    ...restFilter
  }

  let integration = {
    integrationGroup,
    showAllIntegration
  }

  let type = sourceType

  if (model === 'bot') {
    filter.readyMin = isNumber(readyMin) ? readyMin : undefined
    filter.readyMax = isNumber(readyMax) ? readyMax : undefined
    filter.readMin = isNumber(readMin) ? readMin : undefined
    filter.readMax = isNumber(readMax) ? readMax : undefined
    filter.receivedMax = isNumber(receivedMax) ? receivedMax : undefined
    filter.receivedMin = isNumber(receivedMin) ? receivedMin : undefined
  }

  if (model === 'widget') {
    filter.reportMin = isNumber(min) ? min : undefined
    filter.reportMax = isNumber(max) ? max : undefined
  }

  if (model === 'report') {
    const { subLevelType } = sourceFilter || {}
    if (subLevelType === 100101 && !sourceType.includes('Community')) {
      type.push('Community')
    }
    omit(filter, ['subLevelType', 'excludeSubLevelType'])
  }

  if (model === 'dataset') {
    filter.widgetMin = isNumber(widgetMin) ? widgetMin : undefined
    filter.widgetMax = isNumber(widgetMax) ? widgetMax : undefined
  }

  set(filter, 'type', type)

  return {
    integration,
    order,
    filter,
    myFavorite,
    ...rest
  }
}

export const reconFast = (value, model = 'widget') => {
  const {
    filter: sourceFilter,
    integration,
    order,
    name,
    pagination,
    integrationGroup: _integrationGroup,
    showAllIntegration: _showAllIntegration,
    myFavorite: _myFavorite,
    ...rest
  } = cloneDeep(value) || {}

  const {
    createDate,
    lastSyncDate,
    updateDate,
    reportMin = '',
    reportMax = '',
    widgetMin = '',
    widgetMax = '',
    readyMin,
    readyMax,
    readMin,
    readMax,
    receivedMax,
    receivedMin,
    ...restFilter
  } = sourceFilter || {}

  const { pageSize = 100 } = pagination || {}

  const filter = {
    createDate: transformDateToObject(createDate),
    lastSyncDate: transformDateToObject(lastSyncDate),
    updateDate: transformDateToObject(updateDate),
    ...restFilter
  }

  if (model === 'widget') {
    filter.reportNum = { min: reportMin, max: reportMax }
  }
  if (model === 'bot') {
    filter.read = { min: readMin, max: readMax }
    filter.ready = { min: readyMin, max: readyMax }
    filter.received = { min: receivedMin, max: receivedMax }
  }
  if (model === 'dataset') {
    filter.modelNum = { min: widgetMin, max: widgetMax }

  }

  const { integrationGroup = [], showAllIntegration = false } = integration
  return {
    integrationGroup,
    showAllIntegration,
    order,
    filter,
    current: 1,
    size: pageSize,
    name: '',
    ...rest
  }
}

export const parseSearch = (value, model = 'widget') => {
  const {
    filter: sourceFilter,
    integration,
    order,
    myFavorite,
    pagination,
    ...rest
  } = cloneDeep(value) || {}

  const {
    createDate,
    lastSyncDate,
    updateDate,
    reportMin = '',
    reportMax = '',
    widgetMin = '',
    widgetMax = '',
    readyMin,
    readyMax,
    readMin,
    readMax,
    receivedMax,
    receivedMin,
    type: sourceType = [],
    excludeSubLevelType: _excludeSubLevelType,
    subLevelType: _subLevelType,
    ...restFilter
  } = sourceFilter || {}

  const { showAllIntegration = false, integrationGroup = [] } =
    integration || {}
  const { currentPage = 1, pageSize } = pagination || {}

  const filter = {
    createDate: transformDateToObject(createDate),
    lastSyncDate: transformDateToObject(lastSyncDate),
    updateDate: transformDateToObject(updateDate),
    ...restFilter
  }

  if (model === 'widget') {
    filter.reportNum = { min: reportMin, max: reportMax }
  }
  if (model === 'bot') {
    filter.read = { min: readMin, max: readMax }
    filter.ready = { min: readyMin, max: readyMax }
    filter.received = { min: receivedMin, max: receivedMax }
  }
  if (model === 'dataset') {
    filter.modelNum = { min: widgetMin, max: widgetMax }
  }
  let type = sourceType
  if (model === 'report') {
    if (sourceType.includes('Community')) {
      type = difference(sourceType, ['Community'])
      set(filter, 'subLevelType', 100101)
    } else if (sourceType.includes('Template')) {
      set(filter, 'excludeSubLevelType', 100101)
    }
  }

  set(filter, 'type', type)
  return {
    ...rest,
    filter,
    order,
    showAllIntegration,
    integrationGroup,
    myFavorite,
    size: pageSize,
    current: currentPage
  }
}
